# -*- coding: utf-8 -*-
# Pure SQL (ADBAPI) insertion

from twisted.internet.protocol import Factory
from twisted.enterprise import adbapi
from datetime import datetime


class SQLFactory(Factory):
    
    def insertEntry(self, values):
        
        values.update({'date': datetime.strftime('%Y-%m-%d', values['time']),
                       'time': datetime.strftime('%H:%M:%S', values['time']),
                       'pid': values[pid] or 'NULL'})
        
        self.passQuery("INSERT INTO `grdlog_event` "
                       "(`date`, `time`, `host`,"
                       " `program`, `pid`, `severity`,"
                       " `facility`, `message`) VALUES"
                       " (%(date)r, %(time)r, '%(host)s',"
                       " %(program)r, %(pid)s, %(severity)s,"
                       " %(facility)s, %(message)r)" % values)
    
    def passQuery(self, query):
        self.pool.runOperation(query)
    
    def __init__(self, lib, **dsn):
        self.pool = adbapi.ConnectionPool(lib, **dsn)
